package com.crazy.web.controller.outer;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crazy.model.major.dto.ArticleDto;
import com.crazy.model.major.dto.UserSession;
import com.crazy.model.major.entity.TSurvey;
import com.crazy.model.major.entity.TSurveyTime;
import com.crazy.model.major.service.TSurveyTimeService;
import com.crazy.rest.RestResult;
import com.crazy.web.controller.BaseController;
import com.crazy.web.service.outer.ShowService;
import com.crazy.web.service.outer.SurveyService;
import com.crazy.web.service.share.ArticleService;
import com.crazy.web.service.user.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 外部展示
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/18 15:16
 */
@Slf4j
@Controller
@RequestMapping("/outer/show")
public class ShowController extends BaseController {

    @Autowired
    private ShowService showService;
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private SurveyService surveyService;
    @Autowired
    private TSurveyTimeService tSurveyTimeService;

    @GetMapping(value = "/article/{articleId}")
    public String article(@PathVariable Long articleId) {
        ArticleDto articleDto = showService.getCanShowArticleDetail(articleId);
        if(articleDto!=null){
            if(articleDto.getUserId()!=null) {
                UserSession userSession = userService.getUserSession(articleDto.getUserId());
                articleService.hasRead(articleDto.getId());
                request.setAttribute("user", userSession);
            }
            request.setAttribute("article", articleDto);
            request.setAttribute("labels", Arrays.asList(articleDto.getLabelNames().split(",")));
        }
        return "/show/article";
    }

    /**
     * 问卷开始时间
     */
    private Date start = DateUtil.parse("20201221000000", "yyyyMMddHHmmss");
    /**
     * 问卷结束时间
     */
    private Date end = DateUtil.parse("20201225000000", "yyyyMMddHHmmss");

    @GetMapping(value = "/test")
    public String article() {
        request.setAttribute("msg", validateTime());
        return "/show/test";
    }

    @ResponseBody
    @PostMapping(value = "/checktelephone")
    public Map<String, Boolean> checktelephone(@RequestParam String telephone) {
        Map<String, Boolean> result = new HashMap<>();
        result.put("valid", surveyService.checkTelephone(telephone));
        return result;
    }

    @ResponseBody
    @Transactional
    @PostMapping(value = "/add")
    public RestResult add(@RequestBody TSurvey survey) {
        String msg = validateTime();
        if(StrUtil.isNotEmpty(msg)){
            return RestResult.createFailedResult(msg);
        }
        surveyService.add(survey);
        return RestResult.createSuccessResult("提交成功");
    }

    /**
     * 校验时间
     */
    private String validateTime() {
        QueryWrapper<TSurveyTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(TSurveyTime.Columns.id);
        List<TSurveyTime> surveyTimes = tSurveyTimeService.list(queryWrapper);
        if(surveyTimes==null || surveyTimes.isEmpty()){return null;}
        LocalDateTime curr = LocalDateTime.now();
        if(curr.isBefore(surveyTimes.get(0).getStartTime())){
            return "调查问卷还未开始";
        }
        if(curr.isAfter(surveyTimes.get(0).getEndTime())){
            return "调查问卷已结束";
        }
        return null;
    }

}
